package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sikaryofficial.backend.domain.entity.ActivityPrizeRelation;
import com.sikaryofficial.backend.domain.vo.ActivityResultCacheVO;
import com.sikaryofficial.backend.domain.vo.PointConsumerVO;
import com.sikaryofficial.backend.mapper.ActivityPrizeRelationMapper;
import com.sikaryofficial.backend.service.IActivityPrizeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 活动奖品Service业务层处理
 *
 * @author qinjinyuan
 * @date 2024-07-15
 */
@Service
public class ActivityPrizeRelationServiceImpl extends ServiceImpl<ActivityPrizeRelationMapper, ActivityPrizeRelation> implements IActivityPrizeRelationService {
    private static final int PRIZE_COUNT_THRESHOLD = 100;
    @Autowired
    private ActivityPrizeRelationMapper activityPrizeRelationMapper;

    /**
     * 根据礼品ID获取有关联的礼品ID
     *
     * @param prizeIds
     * @return
     */
    @Override
    public Set<Long> getRelationPrizeId(List<Long> prizeIds) {
        List<ActivityPrizeRelation> activityPrizeRelations = this.list(
                new LambdaQueryWrapper<ActivityPrizeRelation>()
                        .select(ActivityPrizeRelation::getPrizeId)
                        .eq(ActivityPrizeRelation::getDeletedVersion, 0L)
                        .in(ActivityPrizeRelation::getPrizeId, prizeIds));
        if (CollUtil.isEmpty(activityPrizeRelations)) {
            return Sets.newHashSet();
        }
        return activityPrizeRelations.stream().map(ActivityPrizeRelation::getPrizeId).collect(Collectors.toSet());
    }

    /**
     * 检查是否关联了100个奖品
     *
     * @param activityId
     * @return
     */
    @Override
    public boolean checkIfRelatedPrizesExceedOneHundred(Long activityId) {
        double winRatio = activityPrizeRelationMapper.checkWinRatioRelation(activityId);
        BigDecimal winRatioBD = BigDecimal.valueOf(winRatio).setScale(0, RoundingMode.HALF_UP);
        return winRatioBD.compareTo(BigDecimal.valueOf(PRIZE_COUNT_THRESHOLD)) == 0;
    }

    /**
     * 根据活动ID集合获取所有关联奖项信息列表
     *
     * @param activityIds 活动ID集合
     * @return 关联奖项信息列表
     */
    @Override
    public List<PointConsumerVO> getPointConsumerList(List<Long> activityIds) {
        return activityPrizeRelationMapper.getPointConsumerList(activityIds);
    }
    /**
     * 获取已中实物奖结果数据
     * @param activityIds
     * @return
     */
    @Override
    public List<ActivityResultCacheVO> getActivityResultCacheList(List<Long> activityIds) {
        return activityPrizeRelationMapper.getActivityResultCacheList(activityIds);
    }

    @Override
    public List<PointConsumerVO> getPointConsumer(Long activityId) {
        List<Long> activityIds = Lists.newArrayList();
        activityIds.add(activityId);
        return getPointConsumerList(activityIds);
    }
}
